package cn.evilelf.math;

import cn.evilelf.sort.Quick;

public class Stat {

	public static int sum(int[] array) {
		int sum = 0;
		for (int i = 0; i < array.length; ++i) {
			sum += array[i];
		}
		return sum;
	}

	public static long sum(long[] array) {
		long sum = 0;
		for (int i = 0; i < array.length; ++i) {
			sum += array[i];
		}
		return sum;
	}

	public static float sum(float[] array) {
		float sum = 0;
		for (int i = 0; i < array.length; ++i) {
			sum += array[i];
		}
		return sum;
	}

	public static double sum(double[] array) {
		int sum = 0;
		for (int i = 0; i < array.length; ++i) {
			sum += array[i];
		}
		return sum;
	}

	public static int mean(int[] array) {
		return sum(array) / array.length;
	}

	public static long mean(long[] array) {
		return sum(array) / array.length;
	}

	public static float mean(float[] array) {
		return sum(array) / array.length;
	}

	public static double mean(double[] array) {
		return sum(array) / array.length;
	}

	public static double variance(double[] array) {
		double tmp = 0;
		double mean = mean(array);
		for (int i = 0; i < array.length; ++i) {
			tmp += (array[i] - mean) * (array[i] - mean);
		}
		return tmp / (array.length - 1);
	}

	public static double std(double[] array) {
		return Math.sqrt(variance((double[]) array));
	}

	public static int median(int[] array) {
		int median;
		Quick.sort(array);
		int length = array.length;
		if (length % 2 != 0) {
			median = array[length / 2];
		} else {
			median = (array[length / 2] + array[length / 2 - 1]) / 2;
		}
		return median;
	}

	public static long median(long[] array) {
		long median;
		Quick.sort(array);
		int length = array.length;
		if (length % 2 != 0) {
			median = array[length / 2];
		} else {
			median = (array[length / 2] + array[length / 2 - 1]) / 2;
		}
		return median;
	}

	public static float median(float[] array) {
		float median;
		Quick.sort(array);
		int length = array.length;
		if (length % 2 != 0) {
			median = array[length / 2];
		} else {
			median = (array[length / 2] + array[length / 2 - 1]) / 2;
		}
		return median;
	}

	public static double median(double[] array) {
		double median;
		Quick.sort(array);
		int length = array.length;
		if (length % 2 != 0) {
			median = array[length / 2];
		} else {
			median = (array[length / 2] + array[length / 2 - 1]) / 2;
		}
		return median;
	}

}
